<?php

namespace app\api\controller;

use app\BaseController;
use app\api\model\UserModel;
use app\api\validate\UserValidate;
use think\facade\Request;

class User extends BaseController
{
    public function register()
    {
        // 获取请求数据
        $data = Request::post();

        // 验证数据
        $validate = new UserValidate();
        if (!$validate->check($data)) {
            return error($validate->getError());
        }

        // 生成随机盐值
        $salt = createRandStr(8);

        // 密码加密使用 MD5 并加盐
        $data['password'] = md5($data['password'] . $salt);
        $data['salt'] = $salt;
        //入库
        if(UserModel::create($data)){
            return success('注册成功');
        }else{
            return error('注册失败');
        }
    }
}